package com.fish.web.controller;

import com.fish.common.annotation.OperationLog;
import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.model.dto.TitleDTO;
import com.fish.model.vo.TitleVO;
import com.fish.service.TitleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 头衔Controller
 */
@Tag(name = "头衔管理", description = "组织头衔管理相关接口")
@Slf4j
@RestController
@RequestMapping("/api/title")
public class TitleController {
    
    @Autowired
    private TitleService titleService;
    
    /**
     * 获取所有头衔列表
     */
    @GetMapping("/list")
    public Result<List<TitleVO>> getAllTitles() {
        log.info("获取所有头衔列表");
        List<TitleVO> titles = titleService.getAllTitles();
        return Result.success(titles);
    }
    
    /**
     * 根据组织ID获取头衔列表
     */
    @GetMapping("/org/{orgId}")
    public Result<List<TitleVO>> getTitlesByOrgId(@PathVariable Long orgId) {
        log.info("获取组织头衔列表，组织ID：{}", orgId);
        List<TitleVO> titles = titleService.getTitlesByOrgId(orgId);
        return Result.success(titles);
    }
    
    /**
     * 根据ID获取头衔详情
     */
    @GetMapping("/{id}")
    public Result<TitleVO> getTitleById(@PathVariable Long id) {
        log.info("获取头衔详情，头衔ID：{}", id);
        TitleVO title = titleService.getTitleById(id);
        return Result.success(title);
    }
    
    /**
     * 创建头衔
     */
    @PostMapping("/create")
    @RequiresPermission("system:title:manage")
    @OperationLog("创建头衔")
    public Result<TitleVO> createTitle(@RequestBody TitleDTO dto) {
        log.info("创建头衔，头衔名称：{}", dto.getTitleName());
        TitleVO title = titleService.createTitle(dto);
        return Result.success("创建成功", title);
    }
    
    /**
     * 更新头衔
     */
    @PutMapping("/update")
    @RequiresPermission("system:title:manage")
    @OperationLog("更新头衔")
    public Result<TitleVO> updateTitle(@RequestBody TitleDTO dto) {
        log.info("更新头衔，头衔ID：{}", dto.getId());
        TitleVO title = titleService.updateTitle(dto);
        return Result.success("更新成功", title);
    }
    
    /**
     * 删除头衔
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermission("system:title:manage")
    @OperationLog("删除头衔")
    public Result<Void> deleteTitle(@PathVariable Long id) {
        log.info("删除头衔，头衔ID：{}", id);
        titleService.deleteTitle(id);
        return Result.success("删除成功", null);
    }
}

